﻿package com.amcharts.chartClasses
{
    import com.amcharts.axes.*;
    import com.amcharts.events.*;
    import com.amcharts.geom.*;
    import com.amcharts.utils.*;
    import flash.display.*;
    import flash.events.*;
    import mx.core.*;
    import mx.styles.*;

    public class AmRectangularChart extends AmCoordinateChart
    {
        protected var _trendLines:Array;
        protected var _chartScrollbar:ChartScrollbar;
        private var _zoomOutText:String = "Show all";
        protected var _horizontalPosition:Number = 0;
        protected var _zoomOutButton:Object;
        protected var _graphsMask:PlotArea;
        protected var _autoMargins:Boolean = true;
        protected var _updateScrollbar:Boolean = true;
        protected var _verticalPosition:Number = 0;
        protected var _mouseWheelHandled:Boolean = false;
        protected var _labelsMask:Rect;
        protected var _mouseWheelEnabled:Boolean = true;
        protected var _scrollbarHeight:Number;
        protected var _dy:Number;
        protected var _heightMultiplyer:Number = 1;
        protected var _dx:Number;
        protected var _zoomOutPressed:Boolean;
        protected var _plotAreaWidth:Number;
        private var _lens:Class;
        protected var _marginsInvalidated:Boolean;
        protected var _plotArea:PlotArea;
        protected var _scrollbarOnly:Boolean;
        protected var _plotAreaHeight:Number;
        protected var _scrollbarPosition:String;
        protected var _widthMultiplyer:Number = 1;
        private static var stylesInited:Boolean = initStyles();

        public function AmRectangularChart()
        {
            _lens = AmRectangularChart__lens;
            _trendLines = new Array();
            return;
        }// end function

        public function get chartScrollbar() : ChartScrollbar
        {
            return _chartScrollbar;
        }// end function

        protected function updateMargins() : void
        {
            _marginTop = getStyle("marginTop") - _dy;
            _marginBottom = getStyle("marginBottom");
            _marginLeft = getStyle("marginLeft");
            _marginRight = getStyle("marginRight");
            return;
        }// end function

        protected function handleZoomOutDown(event:MouseEvent) : void
        {
            _zoomOutPressed = true;
            return;
        }// end function

        protected function handleAxisWidthChange(event:AxisEvent) : void
        {
            var _loc_2:Number = NaN;
            var _loc_3:AxisBase = null;
            var _loc_4:String = null;
            var _loc_5:String = null;
            var _loc_6:Number = NaN;
            var _loc_7:AmChartEvent = null;
            if (this.autoMargins)
            {
                _loc_2 = event.width;
                _loc_3 = AxisBase(event.axis);
                _loc_4 = _loc_3.position;
                _loc_5 = "margin" + Utils.capitalizeFirst(_loc_4);
                _loc_6 = this.getStyle(_loc_5);
                _loc_3.removeEventListener(AxisEvent.AXIS_WIDTH_CHANGED, handleAxisWidthChange);
                if (_loc_2 > _loc_6)
                {
                    this.setStyle(_loc_5, _loc_2);
                    _loc_7 = new AmChartEvent(AmChartEvent.MARGINS_UPDATED);
                    dispatchEvent(_loc_7);
                }
            }
            return;
        }// end function

        protected function updatePlotArea() : void
        {
            _plotAreaWidth = this.width - _marginLeft - _marginRight - _dx;
            _plotAreaHeight = this.height - _marginTop - _marginBottom;
            if (_plotAreaHeight < 1)
            {
                _plotAreaHeight = 1;
            }
            if (_plotAreaWidth < 1)
            {
                _plotAreaWidth = 1;
            }
            _plotArea.x = _marginLeft;
            _plotArea.y = _marginTop;
            _plotArea.fillColors = getStyle("plotAreaFillColors");
            _plotArea.fillAlphas = getStyle("plotAreaFillAlphas");
            _plotArea.borderColor = getStyle("plotAreaBorderColor");
            _plotArea.borderAlpha = getStyle("plotAreaBorderAlpha");
            _plotArea.setSize((_plotAreaWidth + 1), (_plotAreaHeight + 1), _dx, _dy);
            return;
        }// end function

        protected function updateTrendLines() : void
        {
            return;
        }// end function

        public function get zoomOutButton() : Object
        {
            return _zoomOutButton;
        }// end function

        public function get autoMargins() : Boolean
        {
            return _autoMargins;
        }// end function

        protected function handleCursorZoom(event:CursorEvent) : void
        {
            return;
        }// end function

        public function removeTrendLine(param1:TrendLine) : void
        {
            param1.destroy();
            var _loc_2:* = _trendLines.length;
            var _loc_3:* = _loc_2 - 1;
            while (_loc_3 >= 0)
            {
                
                if (_trendLines[_loc_3] == param1)
                {
                    _trendLines.splice(_loc_3, 1);
                }
                _loc_3 = _loc_3 - 1;
            }
            if (_graphsContainer == param1.parent)
            {
                _graphsContainer.removeChild(param1);
            }
            return;
        }// end function

        override protected function updateData() : void
        {
            super.updateData();
            addAxesListeners();
            if (_zoomOutButton)
            {
                _zoomOutButton.visible = false;
            }
            return;
        }// end function

        override protected function createChildren() : void
        {
            var _loc_1:AmSimpleButton = null;
            super.createChildren();
            if (_mouseWheelEnabled)
            {
                this.removeEventListener(MouseEvent.MOUSE_WHEEL, mouseWheelHandler);
                this.addEventListener(MouseEvent.MOUSE_WHEEL, mouseWheelHandler);
            }
            _plotArea = new PlotArea();
            this.addChildAt(_plotArea, 0);
            _graphsMask = new PlotArea();
            this.addChild(_graphsMask);
            _graphsContainer.mask = _graphsMask;
            _labelsMask = new Rect(1, 1, [0], [1]);
            this.addChild(_labelsMask);
            _labelsContainer.mask = _labelsMask;
            if (!_zoomOutButton)
            {
                _loc_1 = new AmSimpleButton();
                _loc_1.includeInLayout = false;
                if (_zoomOutText != "")
                {
                }
                if (_zoomOutText)
                {
                    _loc_1.label = _zoomOutText;
                    _loc_1.icon = _lens;
                    _loc_1.visible = false;
                    _loc_1.addEventListener(MouseEvent.MOUSE_DOWN, handleZoomOutDown);
                    _loc_1.addEventListener(MouseEvent.MOUSE_UP, handleZoomOut);
                }
                addChild(DisplayObject(_loc_1));
                _zoomOutButton = _loc_1;
            }
            return;
        }// end function

        protected function updateGraphs() : void
        {
            var _loc_3:AmGraph = null;
            _graphsContainer.x = _marginLeft + _horizontalPosition;
            _graphsContainer.y = _marginTop + _verticalPosition;
            Utils.removeChildren(_graphsContainer);
            var _loc_1:* = _graphs.length;
            var _loc_2:Number = 0;
            while (_loc_2 < _loc_1)
            {
                
                _loc_3 = _graphs[_loc_2];
                _loc_3.chartType = _chartType;
                _loc_3.width = _plotAreaWidth * _widthMultiplyer;
                _loc_3.height = _plotAreaHeight * _heightMultiplyer;
                _loc_3.dx = _dx;
                _loc_3.dy = _dy;
                _loc_3.index = _loc_2;
                _graphsContainer.addChildAt(_loc_3, 0);
                chooseGraphColor(_loc_3, _loc_2);
                _loc_2 = _loc_2 + 1;
            }
            return;
        }// end function

        public function set zoomOutText(param1:String) : void
        {
            _zoomOutText = param1;
            return;
        }// end function

        public function set zoomOutButton(param1:Object) : void
        {
            if (_zoomOutButton)
            {
                _zoomOutButton.removeEventListener(MouseEvent.MOUSE_UP, handleZoomOut);
                _zoomOutButton.removeEventListener(MouseEvent.MOUSE_DOWN, handleZoomOutDown);
                removeChild(DisplayObject(_zoomOutButton));
            }
            _zoomOutButton = param1;
            _zoomOutButton.visible = false;
            _zoomOutButton.includeInLayout = false;
            _zoomOutButton.addEventListener(MouseEvent.MOUSE_DOWN, handleZoomOutDown);
            _zoomOutButton.addEventListener(MouseEvent.MOUSE_UP, handleZoomOut);
            addChild(DisplayObject(_zoomOutButton));
            return;
        }// end function

        override protected function updateAmChartsLink() : void
        {
            super.updateAmChartsLink();
            if (_amchartsLink)
            {
                _amchartsLink.x = _marginLeft + _dx;
                _amchartsLink.y = _marginTop + _dy;
            }
            return;
        }// end function

        protected function updateCategoryAxis() : void
        {
            return;
        }// end function

        public function get mouseWheelEnabled() : Boolean
        {
            return _mouseWheelEnabled;
        }// end function

        override protected function setAxisRenderers(param1:AxisBase) : void
        {
            param1.axisItemRenderer = RectangularAxisItemRenderer;
            param1.axisRenderer = RectangularAxisRenderer;
            param1.guideFillRenderer = RectangularAxisGuideFillRenderer;
            return;
        }// end function

        public function set chartCursor(param1:ChartCursor) : void
        {
            if (_chartCursor)
            {
                _chartCursor.clean();
                removeChild(_chartCursor);
            }
            _chartCursor = param1;
            if (_chartCursor)
            {
                _dataChanged = true;
                _chartCursor.addEventListener(CursorEvent.CHANGED, handleCursorChange);
                _chartCursor.addEventListener(CursorEvent.ZOOMED, handleCursorZoom);
                addChild(_chartCursor);
            }
            invalidateProperties();
            return;
        }// end function

        public function removeChartCursor() : void
        {
            if (_chartCursor)
            {
                _chartCursor.clean();
                if (_chartCursor.parent == this)
                {
                    removeChild(_chartCursor);
                }
                _chartCursor = null;
                invalidateProperties();
            }
            return;
        }// end function

        protected function updateScrollbars() : void
        {
            return;
        }// end function

        public function destroyAllListeners() : void
        {
            if (_chartCursor)
            {
                _chartCursor.clean();
            }
            if (_chartScrollbar)
            {
                _chartScrollbar.clean();
            }
            return;
        }// end function

        protected function updateDepths() : void
        {
            return;
        }// end function

        override public function hideGraph(param1:AmGraph) : void
        {
            super.hideGraph(param1);
            _updateScrollbar = false;
            return;
        }// end function

        public function addTrendLine(param1:TrendLine) : TrendLine
        {
            _trendLines.push(param1);
            _dataChanged = true;
            invalidateProperties();
            return param1;
        }// end function

        protected function updateMasks() : void
        {
            _graphsMask.x = _marginLeft;
            _graphsMask.y = _marginTop;
            _graphsMask.setSize(_plotAreaWidth, _plotAreaHeight, 0, 0);
            _labelsMask.x = _marginLeft;
            _labelsMask.width = _plotAreaWidth;
            _labelsMask.height = this.height;
            return;
        }// end function

        protected function setAxisRotation(param1:ValueAxis) : void
        {
            return;
        }// end function

        protected function updateZoomOutButton() : void
        {
            if (_zoomOutButton)
            {
                if (_zoomOutButton is UIComponent)
                {
                    _zoomOutButton.validateNow();
                }
                _zoomOutButton.setActualSize(_zoomOutButton.getExplicitOrMeasuredWidth(), _zoomOutButton.getExplicitOrMeasuredHeight() + 8);
                _zoomOutButton.x = _marginLeft + _plotAreaWidth - _zoomOutButton.width;
                _zoomOutButton.y = _marginTop + 1;
            }
            return;
        }// end function

        public function removeChartScrollbar() : void
        {
            if (_chartScrollbar)
            {
                if (_chartScrollbar.parent == this)
                {
                    removeChild(_chartScrollbar);
                }
                _chartScrollbar.destroy();
                _chartScrollbar = null;
                invalidateProperties();
            }
            return;
        }// end function

        public function get zoomOutText() : String
        {
            return _zoomOutText;
        }// end function

        override protected function createChart() : void
        {
            super.createChart();
            if (_scrollbarOnly)
            {
                _valuesContainer.visible = false;
                updateDepths();
                updateDxy();
                updateMargins();
                updatePlotArea();
                updateScrollbars();
                updateZoomOutButton();
            }
            else
            {
                updateDepths();
                updateDxy();
                updateMargins();
                updatePlotArea();
                updateAmChartsLink();
                updateMasks();
                updateScrollbars();
                updateValueAxes();
                updateCategoryAxis();
                updateChartCursor();
                updateGraphs();
                updateTrendLines();
                updateZoomOutButton();
            }
            dispatchDataUpdatedEvent();
            _chartCreated = true;
            return;
        }// end function

        public function invalidateMargins() : void
        {
            _marginsInvalidated = true;
            invalidateDisplayList();
            return;
        }// end function

        protected function updateChartCursor() : void
        {
            if (_chartCursor)
            {
                _chartCursor.x = _marginLeft;
                _chartCursor.y = _marginTop;
                _chartCursor.width = _plotAreaWidth;
                _chartCursor.height = _plotAreaHeight;
                _chartCursor.dx = _dx;
                _chartCursor.dy = _dy;
                _chartCursor.chart = this;
            }
            return;
        }// end function

        protected function mouseWheelHandler(event:MouseEvent) : void
        {
            return;
        }// end function

        public function set chartScrollbar(param1:ChartScrollbar) : void
        {
            if (_chartScrollbar)
            {
                _chartScrollbar.destroy();
                removeChild(_chartScrollbar);
            }
            _chartScrollbar = param1;
            if (_chartScrollbar)
            {
                _dataChanged = true;
                _chartScrollbar.addEventListener(SerialChartEvent.ZOOMED, handleScrollbarZoom);
                addChild(_chartScrollbar);
            }
            invalidateProperties();
            return;
        }// end function

        override public function showGraph(param1:AmGraph) : void
        {
            super.showGraph(param1);
            _updateScrollbar = false;
            return;
        }// end function

        public function set trendLines(param1:Array) : void
        {
            _trendLines = param1;
            invalidateProperties();
            return;
        }// end function

        protected function handleZoomOut(event:MouseEvent) : void
        {
            return;
        }// end function

        public function set mouseWheelEnabled(param1:Boolean) : void
        {
            _mouseWheelEnabled = param1;
            return;
        }// end function

        public function get chartCursor() : ChartCursor
        {
            return _chartCursor;
        }// end function

        public function get trendLines() : Array
        {
            return _trendLines;
        }// end function

        protected function resetMouseWheel() : void
        {
            _mouseWheelHandled = false;
            return;
        }// end function

        protected function handleScrollbarZoom(event:SerialChartEvent) : void
        {
            return;
        }// end function

        protected function updateDxy() : void
        {
            var _loc_1:* = getStyle("angle");
            var _loc_2:* = getStyle("depth3D");
            _dx = _loc_2 * Math.cos(_loc_1 * Math.PI / 180);
            _dy = (-_loc_2) * Math.sin(_loc_1 * Math.PI / 180);
            return;
        }// end function

        public function set autoMargins(param1:Boolean) : void
        {
            _autoMargins = param1;
            invalidateMargins();
            return;
        }// end function

        protected function addAxesListeners() : void
        {
            var _loc_1:Number = NaN;
            var _loc_2:String = null;
            var _loc_3:String = null;
            var _loc_4:Number = NaN;
            var _loc_5:ValueAxis = null;
            if (_autoMargins)
            {
                _loc_1 = _valueAxes.length;
                _loc_4 = 0;
                while (_loc_4 < _loc_1)
                {
                    
                    _loc_5 = _valueAxes[_loc_4];
                    setAxisRotation(_loc_5);
                    if (!_loc_5.ignoreAxisWidth)
                    {
                        if (_loc_5.inside)
                        {
                        }
                        if (!_loc_5.title)
                        {
                        }
                        else
                        {
                            _loc_2 = _loc_5.position;
                            _loc_3 = "margin" + Utils.capitalizeFirst(_loc_2);
                            this.setStyle(_loc_3, 0);
                            _loc_5.removeEventListener(AxisEvent.AXIS_WIDTH_CHANGED, handleAxisWidthChange);
                            _loc_5.addEventListener(AxisEvent.AXIS_WIDTH_CHANGED, handleAxisWidthChange);
                        }
                    }
                    _loc_4 = _loc_4 + 1;
                }
            }
            return;
        }// end function

        protected function updateValueAxes() : void
        {
            var _loc_5:ValueAxis = null;
            var _loc_6:Number = NaN;
            var _loc_1:* = _marginLeft + _horizontalPosition;
            var _loc_2:* = _marginTop + _verticalPosition;
            _valueAxesContainer.x = _loc_1;
            _valueAxesContainer.y = _loc_2;
            _valuesContainer.x = _loc_1;
            _valuesContainer.y = _loc_2;
            Utils.removeChildren(_grid0);
            _grid0.x = _loc_1;
            _grid0.y = _loc_2;
            _labelsContainer.x = _loc_1;
            _labelsContainer.y = _loc_2;
            Utils.removeChildren(_valueAxesContainer);
            var _loc_3:* = _valueAxes.length;
            var _loc_4:Number = 0;
            while (_loc_4 < _loc_3)
            {
                
                _loc_5 = _valueAxes[_loc_4];
                _loc_5.width = _plotAreaWidth * _widthMultiplyer;
                _loc_5.height = _plotAreaHeight * _heightMultiplyer;
                _loc_5.visibleAxisWidth = _plotAreaWidth;
                _loc_5.visibleAxisHeight = _plotAreaHeight;
                _loc_5.visibleAxisX = -_horizontalPosition;
                _loc_5.visibleAxisY = -_verticalPosition;
                _loc_5.dx = _dx;
                _loc_5.dy = _dy;
                if (_loc_5.rotate == false)
                {
                    _loc_6 = _heightMultiplyer;
                }
                else
                {
                    _loc_6 = _widthMultiplyer;
                }
                _loc_5.gridCountReal = Math.floor(_loc_5.getStyle("gridCount") * _loc_6);
                _valueAxesContainer.addChild(_loc_5);
                _loc_4 = _loc_4 + 1;
            }
            return;
        }// end function

        protected function handleCursorChange(event:CursorEvent) : void
        {
            return;
        }// end function

        override protected function updateDisplayList(param1:Number, param2:Number) : void
        {
            super.updateDisplayList(param1, param2);
            if (_marginsInvalidated)
            {
                addAxesListeners();
                _marginsInvalidated = false;
            }
            _updateScrollbar = true;
            return;
        }// end function

        private static function initStyles() : Boolean
        {
            var styleDeclaration:* = StyleManager.getStyleDeclaration("AmRectangularChart");
            if (!styleDeclaration)
            {
                styleDeclaration = new CSSStyleDeclaration();
            }
            styleDeclaration.defaultFactory = function () : void
            {
                this.marginLeft = 80;
                this.marginTop = 15;
                this.marginBottom = 35;
                this.marginRight = 15;
                this.angle = 0;
                this.depth3D = 0;
                return;
            }// end function
            ;
            StyleManager.setStyleDeclaration("AmRectangularChart", styleDeclaration, false);
            return true;
        }// end function

    }
}
